package cn.itcast.jx.service.impl;

import java.io.Serializable;
import java.util.List;

import cn.itcast.jx.dao.BaseDao;
import cn.itcast.jx.domain.Contract;
import cn.itcast.jx.service.ContractService;
import cn.itcast.jx.util.Page;
import cn.itcast.jx.util.UtilFuns;

public class ContractServiceImpl implements ContractService{

	private BaseDao baseDao;

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	@Override
	public Page<Contract> findPage(String hql, Page<Contract> page,
			Class<Contract> entityClass, Object[] params) {
		return baseDao.findPage(hql, page, entityClass, params);
	}

	@Override
	public Contract get(Class<Contract> entityClass, Serializable id) {
		// TODO Auto-generated method stub
		return baseDao.get(entityClass, id);
	}

	@Override
	public void saveOrUpdate(Contract entity) {
		if(UtilFuns.isEmpty(entity.getId())) {
			//新增
			entity.setTotalAmount(0d);
			entity.setState(0);
			baseDao.saveOrUpdate(entity);
			
		}else {
			//修改
			Contract contract = baseDao.get(Contract.class, entity.getId());
			
			contract.setCustomName(entity.getCustomName());
			//打印板式
			contract.setPrintStyle(entity.getPrintStyle());
			//合同号
			contract.setContractNo(entity.getContractNo());
			//收购方
			contract.setOfferor(entity.getOfferor());
			//制单人
			contract.setInputBy(entity.getInputBy());
			//审单人
			contract.setCheckBy(entity.getCheckBy());
			//验货员
			contract.setInspector(entity.getInspector());
			//签期
			contract.setSigningDate(entity.getSigningDate());
			//重要程度
			contract.setImportNum(entity.getImportNum());
			//船期
			contract.setShipTime(entity.getShipTime());
			//贸易条款
			contract.setTradeTerms(entity.getTradeTerms());
			//交期
			contract.setDeliveryPeriod(entity.getDeliveryPeriod());
			//要求
			contract.setCrequest(entity.getCrequest());
			//说明
			contract.setRemark(entity.getRemark());
			
			//更新
			baseDao.saveOrUpdate(entity);
			
		}
	}

	@Override
	public void delete(Class<Contract> entityClass, String[] ids) {
		for (String id : ids) {
			baseDao.deleteById(entityClass, id);
		}
	}

	@Override
	public List<Contract> find(String hql, Class<Contract> entityClass,
			Object[] params) {
		// TODO Auto-generated method stub
		return baseDao.find(hql, entityClass, params);
	}
	
}
